package com.mt.course.service.impl;

import java.util.List;
import java.util.Map;


import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.course.api.service.IDatumService;
import com.mt.course.entity.DatumEntity;
import com.mt.course.service.model.DatumServiceModel;
import org.springframework.stereotype.Service;

@Service
public class DatumServiceImpl implements IDatumService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DatumServiceImpl.class);

    @Autowired
    private DatumServiceModel datumServiceModel;

    public ServiceResult<DatumEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<DatumEntity> result = null;
        // 该参数标识前段是否需要权限数据
        try {
            result = datumServiceModel.selectInfo(params);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<DatumEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<DatumEntity>> result = new ServiceResult<List<DatumEntity>>();
        try {
            result = datumServiceModel.selectList(params, pagerInfo);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> findCount(Map<String, Object> params) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = datumServiceModel.selectCount(params);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][findCount]：query findCount occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doInsert(DatumEntity datumEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = datumServiceModel.insert(datumEntity);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(DatumEntity datumEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = datumServiceModel.updateRelate(datumEntity);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer datumId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = datumServiceModel.deleteByPrimaryKey(datumId);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(DatumEntity datumEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = datumServiceModel.updateByPrimaryKeySelective(datumEntity);
        } catch (Exception e) {
            LOGGER.error("[DatumServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
